<?php
/**
 * Created by PhpStorm.
 * User: tusimo
 * Date: 10/28/19
 * Time: 7:02 PM
 */

if (!function_exists('conf')) {
    /**
     * Get / set the specified configuration value.
     *
     * If an array is passed as the key, we will assume you want to set an array of values.
     *
     * @param  array|string|null $key
     * @param  mixed $default
     * @return mixed
     * @throws Exception
     */
    function conf($key = null, $default = null)
    {
        if (is_null($key)) {
            return \Melo\Config\Container::instance()->all();
        }

        if (is_array($key)) {
            return \Melo\Config\Container::instance()->set($key);
        }

        return \Melo\Config\Container::instance()->get($key, $default);
    }
}

if (!function_exists('parse_cli_params')) {
    /**
     * 解析命令行参数
     * @param $argv
     * @return array
     */
    function parse_cli_params($argv)
    {
        unset($argv[0]);
        $params = [];
        foreach ($argv as $value) {
            [$config, $value] = explode('=', $value);
            $params[$config] = $value;
        }
        return $params;
    }
}

if (!function_exists('is_json')) {
    /**
     * 检测字符串是否是json
     * @param $string
     * @return bool
     */
    function is_json($string)
    {
        json_decode($string);
        return (json_last_error() == JSON_ERROR_NONE);
    }
}

if (!function_exists('reload_jobs')) {
    /**
     * reload pm2 job 
     */
    function reload_jobs()
    {
        if (!function_exists('exec')) {
            echo 'exec is not exists, please install exec';
            return false;
        }
        $dockerPath =  __DIR__ . '/../../../../docker';

        $jobJson = $dockerPath . '/pm2/jobs.json';

        if (!is_production()) {
            $jobJson = $dockerPath . '/pm2/apps.json';
        }
        if (file_exists($jobJson)) {
            $json_string = file_get_contents($jobJson);
            $jobs = json_decode($json_string, true);
            if (!$jobs || !isset($jobs['apps'])) {
                echo 'jobs file is  not valid';
                return false;
            }

            $re = [];
            foreach ($jobs['apps'] as $job) {
                if ($job && isset($job['name'])) {
                    echo 'reloading job:' . $job['name'] . PHP_EOL;
                    exec('pm2 reload ' . $job['name'], $re);
                }
            }
            return true;
        }
        echo 'jobs file not exists';
        return false;
    }
}

if (!function_exists('environment')) {
    /**
     * Return current environment
     *
     * @return string
     */
    function environment()
    {
        $env = getenv("APP_ENV");
        if (false === $env) {
            if (defined('ENVIRONMENT')) {
                return ENVIRONMENT;
            }
            $environment = ini_get('yaf.environ');
            if ($environment == '' || $environment == 'product') {
                return 'production';
            }
            return $environment;
        }
        return $env;
    }
}

if (!function_exists('is_production')) {
    /**
     * Return current environment is production or not
     *
     * @return boolean
     */
    function is_production()
    {
        return environment() === 'production' || environment() == 'product';
    }
}